#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Q1mi"
# Date: 2018/10/25

##爬取中国天气网未来40天的福建9大地区的天气数据
##http://www.weather.com.cn/weather40d/101230103.shtml

import requests
import re
import os
import pandas as pd
import json
import time

import pymssql
import requests
# from code import *
from datetime import datetime,timedelta
from dateutil.relativedelta import relativedelta

def weater_ch(x):
    '''

    :param x:
    :return:  数字转化为中文
    '''
    # 已经确定的有：'00':'晴','01':'多云','02':'阴','07':'小雨','08':'中雨','09':'大雨','301':'雨'
    c1_dic = {'00': '晴', '01': '多云', '02': '阴', '03': '阵雨', '04': '雷阵雨', '05': '雷阵雨', '06': '暴雨', '07': '小雨',
              '08': '中雨', '09': '大雨', '301': '雨'}

    if len(x) > 0:
        if x in c1_dic.keys():
            c1_w = c1_dic[x]
        else:
            c1_w = '多云'
    else:
        c1_w = x
    return c1_w


def weater_zh(c1, c2):
    '''

    :return:  结合白天和晚上的气象中文说明
    '''
    word = ''
    if not c2:
        if c1 != c2:
            word += c1
            word += '转'
            word += c2
    else:
        word = c1
    return c1


class Spider(object):
    '''
    address_st:目的地的code
    time_start：出发时间
    time_end：结束时间
    #获取dataframe 【最高温，最低温，天气情况，预测时间，更新时间】
    '''

    def __init__(self, address_st, time_start, time_end):
        self.headers = {"Accept": "*/*",
                        "Accept-Encoding": "gzip, deflate",
                        "Accept-Language": "zh-CN,zh;q=0.9",
                        'Connection': 'keep-alive',
                        'Cookie': 'f_city=%E7%A6%8F%E5%B7%9E%7C101230101%7C; vjuids=74c546de1.165b2f1c48f.0.8fb370cd5c77; UM_distinctid=165b31b2f39255-02c904974464eb-9393265-1fa400-165b31b2f3dbe4; vjlast=1536305514.1536541371.13; Hm_lvt_080dabacb001ad3dc8b9b9049b36d43b=1536305514,1536541371; Wa_lvt_1=1536305514,1536541372; Wa_lpvt_1=1536541372; Hm_lpvt_080dabacb001ad3dc8b9b9049b36d43b=1536541422',
                        "Host": 'd1.weather.com.cn',
                        'Referer': 'http://www.weather.com.cn/weather40d/101230103.shtml',
                        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
                        }
        self.address_st = address_st
        self.time_start = time_start
        self.time_end = time_end
        ##可取的时间最远天气数据
        self.now_time = time.strftime('%Y%m%d', time.localtime(time.time()))
        self.now_time_40 = (datetime.now() + timedelta(days=+39)).strftime("%Y%m%d")  # 14 未来15天的，，，39：未来40天
        if self.time_end > self.now_time_40:
            self.time_end = self.now_time_40

    def month(self):
        month_list = [self.time_start[:6]]

        if self.time_start[:6] != self.time_end[:6]:

            if int(self.time_start[-4:-2]) <= 10:
                for x in range(int(self.time_start[4:6]) + 1, int(self.time_end[4:6]) + 1):
                    month_list.append(self.time_start[:4] + str(x))
            else:
                date_time = datetime.strptime(self.time_start, '%Y%m%d')
                add_time = (date_time + relativedelta(months=+1)).strftime("%Y%m")
                month_list.append(add_time)

                if add_time != self.time_end[:6]:
                    add_time1 = (date_time + relativedelta(months=+2)).strftime("%Y%m")
                    month_list.append(add_time1)
        return month_list

    def spider_weater(self):

        month_list = self.month()
        weater_list = set()

        for month in month_list:
            home_page_url = 'http://d1.weather.com.cn/calendar_new/{}/{}_{}.html?_=1536541421639'.format(
                month[:4], self.address_st, month)

            response = requests.get(url=home_page_url, headers=self.headers).content.decode('utf8')

            # 转化为字典格式
            reg = re.sub(r'var fc40 = ', '', response)
            context_list = json.loads(reg)

            for x in context_list:

                if x['date'] > self.time_end:
                    break

                if self.time_start <= x['date'] <= self.time_end:
                    context_lis = []
                    context_lis.append(x['max'])  # 最高温
                    context_lis.append(x['min'])  # 最低温
                    context_lis.append(x.get('w1'))  # 预测气温情况
                    # context_lis.append(x.get('wd1'))  # 预测风力
                    # rain1 = x.get('rain1')
                    # rain2 = x.get('rain2')
                    # if not rain1 :
                    #     context_lis.append('')  # 预测最大降水量
                    #     context_lis.append('')  # 预测最小降水量
                    # else:
                    #     if float(rain1) >= float(rain2):
                    #         context_lis.append(rain1)  # 预测最大降水量
                    #         context_lis.append(rain2)  # 预测最小降水量
                    #     else:
                    #         context_lis.append(rain2)  # 预测最大降水量
                    #         context_lis.append(rain1)  # 预测最小降水量
                    w_c1 = x.get('c1')
                    w_c2 = x.get('c2')
                    # context_lis.append(w_c1)  # 天气情况 --- 晴天天气
                    # context_lis.append(w_c2)  # 天气情况 --- 夜间天气
                    if not x.get('w1'):
                        wd_zh = ''
                        w_c1_zh = weater_ch(str(w_c1))
                        if w_c2:
                            w_c2_zh = weater_ch(str(w_c1))
                            wd_zh = weater_zh(w_c1_zh, w_c2_zh)
                        else:
                            wd_zh = w_c1_zh
                        context_lis[2] = wd_zh
                    context_lis.append(x['date'])  # 预测的时间---YBTM
                    context_lis.append(time.strftime('%Y-%m-%d %H:00:00', time.localtime()))  # 保存入数据库时间---PSTM
                    # context_lis.append(time.strftime('%Y-%m-%d ', time.localtime(time.time())) + x['time'])  # 更新时间--RSTM

                    weater_list.add(tuple(context_lis))
        # 排序
        weater_list_sort = list(weater_list)
        weater_list_sort.sort(key=lambda x: x[-2])
        # 转化为dataframe
        df2 = pd.DataFrame(weater_list_sort, columns=['wd_max', 'wd_min', 'weater', 'YBTM', 'PSTM'])
        return df2



